package com.xindaoapp.happypet.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.xindaoapp.happypet.R;

public class MyVoiceDialog extends Dialog {
    private final Context context;
    private View makeSure, cancel;
    private InitListener initListener;
    private RecognizerDialogListener recognizerDialogListener;

    public MyVoiceDialog(Context context, InitListener initListener) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.initListener = initListener;
        mIat = SpeechRecognizer.createRecognizer(context, initListener);

    }

    public MyVoiceDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_voicedialog);
        makeSure = this.findViewById(R.id.makeSure);
        cancel = this.findViewById(R.id.cancel);

        makeSure.setOnClickListener(mOnClickListener);
    }

    int ret = 0;// 函数调用返回值
    private final View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.makeSure:
                    ret = mIat.startListening(recognizerListener);
                    if (ret != ErrorCode.SUCCESS) {
                        // "听写失败,错误码：";
                    } else {
                        // showTip(getString(R.string.text_begin));
                    }
                    break;
                case R.id.cancel:

                    break;

            }
        }


    };
    private SpeechRecognizer mIat;


    public void setRecognizerDialogListener(RecognizerDialogListener recognizerDialogListener) {
        this.recognizerDialogListener = recognizerDialogListener;
    }


    /** 听写监听器。 */
    private final RecognizerListener recognizerListener = new RecognizerListener() {

        @Override
        public void onBeginOfSpeech() {}


        @Override
        public void onError(SpeechError error) {}

        @Override
        public void onEndOfSpeech() {}



        @Override
        public void onResult(RecognizerResult results, boolean isLast) {

        }

        @Override
        public void onVolumeChanged(int volume) {}


        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {}
    };

}
